"""
    要求用户输入姓名、年龄、手机号、身份证号、所选课程，完成注册
    手机号、身份证号唯一
    可选的课程只能从Python、Linux、网络安全、前端
    学员信息存入文件
"""
# 1、先让用户输入信息，将手机号、身份证号拿出来和文件里的相比 判断唯一性
# 2、怎么可选？可选性判断！


def register_api():
    stu_data = {}
    print("欢迎来到路飞学园".center(50, "-"))
    print("请完成学籍注册")
    name = input("请输入姓名：").strip()
    age = input("请输入年龄：").strip()
    phone = input("请输入手机号：").strip()
    if phone in phone_list:
        exit("手机号码已被注册")
    id_card = input("请输入身份证号：").strip()
    if id_card in id_card_list:
        exit("身份证号码已被注册")
    course_list = ["Python开发", "Linux云计算", "网络安全", "数据分析&机器学习"]
    for index, courses in enumerate(course_list):
        print(f"{index}.{courses}")
    selected_num = input("请选择喜欢的课：")
    if selected_num.isdigit():
        selected_num = int(selected_num)
        if 0 <= selected_num < len(course_list):
            selected_course = course_list[selected_num]
        else:
            exit("输入选项非法!")
    else:
        exit("非法输入！")
    # 将获取的数据给返回来
    stu_data["name"] = name
    stu_data["age"] = age
    stu_data["phone"] = phone
    stu_data["id_card"] = id_card
    stu_data["selected_course"] = selected_course
    return stu_data


def commit_to_db(filename, stu_data):
    """
    将获取的数据保存到文件里
    :param filename: 文件名
    :param stu_data: 数据
    :return:
    """
    f = open(filename, "a")
    row = f"{stu_data['name']},{stu_data['age']},{stu_data['phone']},{stu_data['id_card']},{stu_data['selected_course']}\n"
    f.write(row)
    f.close()


def load_validate_data(filename):
    f = open(filename)
    phone_list = []
    id_card_list = []
    for line in f:
        line = line.strip().split(",")
        phone = line[2]
        id_card = line[3]
        phone_list.append(phone)
        id_card_list.append(id_card)
    return phone_list,id_card_list


file_name = "students_info.txt"
phone_list,id_card_list = load_validate_data(file_name)
student_data = register_api()
commit_to_db(file_name, student_data)

